//
//  FWHttpUtil.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/9.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWHttpUtil.h"
#import <AFNetworking.h>

@interface FWHttpUtil ()
@property (strong, nonatomic) AFHTTPSessionManager *manager;
@end

@implementation FWHttpUtil

+ (instancetype)sharedHttpUtil{
    static FWHttpUtil *sharedClass;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClass = [[FWHttpUtil alloc] init];
    });
    return sharedClass;
}

+(void)cancelManager{
    [[FWHttpUtil sharedHttpUtil].manager.tasks makeObjectsPerformSelector:@selector(cancel)];
}

- (AFHTTPSessionManager *)manager{
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

+ (NSURLSessionDataTask *)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[FWHttpUtil sharedHttpUtil].manager GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    return task;
}

+ (NSURLSessionDataTask *)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[FWHttpUtil sharedHttpUtil].manager POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    return task;
}
@end
